package com.gzhryc.html;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.json.JsonTools;
import com.gzhryc.html.ViewHelper.WebsiteDataWrap;
import com.gzhryc.html.dao.db.Website;
import com.gzhryc.html.dao.db.WebsitePage;
import com.gzhryc.html.models.PageBanners;
import com.gzhryc.html.models.PageData;
import com.gzhryc.servlet.Resource;
import com.gzhryc.servlet.resources.VelocityResource;

import eu.bitwalker.useragentutils.UserAgent;

public class ViewResource extends Resource{
	
	private String htmlTheme;		//主题
	private String path;			//路径
	private boolean isUrlFirst;
	
	public ViewResource() {this.path = null;}
	
	public ViewResource(String path) {
		this.path = path;
		this.isUrlFirst = true;
	}
	
	public ViewResource(String path,boolean isUrlFirst) {
		this.path = path;
		this.isUrlFirst = isUrlFirst;
	}
	
	public ViewResource(String htmlTheme,String path) {
		this.htmlTheme = htmlTheme;
		this.path = path;
		this.isUrlFirst = true;
	}
	
	public ViewResource(String htmlTheme,String path,boolean isUrlFirst) {
		this.htmlTheme = htmlTheme;
		this.path = path;
		this.isUrlFirst = isUrlFirst;
	}

	@Override
	public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//TODO 临时从数据库中获取
		if(StringUtils.isNotBlank(htmlTheme)){
			List<Website> websiteList = WebsiteService.self().findByHtmlTheme(htmlTheme);
			if(websiteList != null && websiteList.size() > 0) {
				Website website = websiteList.get(0);
				WebsitePage page = WebsitePageService.self().getByWebsiteIdAndPath(website.getId(), path);
				if(page != null) {
					PageData pageData = new PageData();
					pageData.setTitle(pageData.getTitle());
					if(StringUtils.isNotBlank(page.getBanners())) {
						PageBanners banners = JsonTools.fromJson(page.getBanners(), PageBanners.class);
						pageData.setBanners(banners.getItems());
					}
					pageData.setTemplatePath("/" + htmlTheme + "/html/" + path);
					VelocityResource resource = new VelocityResource(pageData.getTemplatePath());
					resource.export(request, response);
				}
			}
		}else {
			String contextPath = request.getContextPath();
			UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
			String lang = request.getLocale().getLanguage();
			String url = request.getRequestURI();
			if(StringUtils.isNotBlank(contextPath) && !contextPath.equals("/")) {
				url = url.replaceFirst(contextPath, "");
			}
			List<WebsiteDataWrap> tempCache = ViewHelper.findWebsiteData(contextPath,userAgent,lang);
			PageData page = null;
			if(isUrlFirst) {
				page = ViewHelper.getConfigPage(tempCache,url);
			}else {
				page = ViewHelper.getConfigPage(tempCache, path);
			}
			if(page == null){
				page = ViewHelper.getPage(tempCache, path);
			}
			if(page != null) {
				VelocityResource resource = new VelocityResource(page.getTemplatePath());
				resource.export(request, response);
			}
		}
	}
}
